﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using System.Web.Script.Serialization;

using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.BusinessService;

namespace Fuse8.ContentManagementFramework.Domain
{
    public partial class CmsEntityMeta
    {
        private string metaFieldName;

        [XmlIgnore]
        [ScriptIgnore]
        public string MetaFieldName
        {
            get
            {
                return metaFieldName ?? (metaFieldName = EntityMetaFieldLink.EntityMetaField.MetaFieldName);
            }
            internal set { metaFieldName = value; }
        }

        [XmlIgnore]
        [ScriptIgnore]
        public string MetaFieldType
        {
            get
            {
                return EntityMetaFieldLink.EntityMetaField.MetaFieldType;
            }
        }

        [XmlIgnore]
        [ScriptIgnore]
        public bool IsDefault { get; set; }
        
        public IList<CmsEntityMeta> GetEntityMetas(int entityId, string entityType)
        {
            Guard.ArgumentNotEmpty(entityType, "entityType");

            IEnumerable<CmsEntityMeta> result = null;

            if (entityId > 0)
            {
                result = new BusinessServiceFacade<CmsEntityMeta, int>()
                    .Find(p => p.EntityId == entityId && p.EntityMetaFieldLink.EntityTypeParameter == entityType);
            }

            return result.ToList();
        }
    }
}
